{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Threshold Activation function\n",
    "def threshold (x):\n",
    "    cond = tf.less(x, tf.zeros(tf.shape(x), dtype = x.dtype))\n",
    "    out = tf.where(cond, tf.zeros(tf.shape(x)), tf.ones(tf.shape(x)))\n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Hyper parameters\n",
    "eta = 0.4  # learning rate parameter\n",
    "epsilon = 1e-03 # minimum accepted error\n",
    "max_epochs = 100 # Maximum Epochs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Training Data  Y = AB + BC, sum of two linear functions.\n",
    "T, F = 1., 0.\n",
    "X_in = [\n",
    "    [T, T, T, T],\n",
    "    [T, T, F, T],\n",
    "    [T, F, T, T],\n",
    "    [T, F, F, T],\n",
    "    [F, T, T, T],\n",
    "    [F, T, F, T],\n",
    "    [F, F, T, T],\n",
    "    [F, F, F, T],\n",
    "    ]\n",
    "Y = [\n",
    "    [T],\n",
    "    [T],\n",
    "    [F],\n",
    "    [F],\n",
    "    [T],\n",
    "    [F],\n",
    "    [F],\n",
    "    [F]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 1  mean error: 0.625\n",
      "epoch: 2  mean error: 0.125\n",
      "epoch: 3  mean error: 0.125\n",
      "epoch: 4  mean error: 0.375\n",
      "epoch: 5  mean error: 0.125\n",
      "epoch: 6  mean error: 0.125\n",
      "epoch: 7  mean error: 0.375\n",
      "epoch: 8  mean error: 0.125\n",
      "epoch: 9  mean error: 0.125\n",
      "epoch: 10  mean error: 0.125\n",
      "epoch: 11  mean error: 0.125\n",
      "epoch: 12  mean error: 0.0\n",
      "Training complete\n"
     ]
    }
   ],
   "source": [
    "W = tf.Variable(tf.random_normal([4,1], stddev=2, seed = 0))\n",
    "h = tf.matmul(X_in, W) \n",
    "Y_hat = threshold(h)\n",
    "error = Y - Y_hat\n",
    "mean_error = tf.reduce_mean(tf.square(error))\n",
    "dW =  eta * tf.matmul(X_in, error, transpose_a=True)\n",
    "train = tf.assign(W, W+dW)\n",
    "init = tf.global_variables_initializer()\n",
    "err = 1\n",
    "epoch = 0\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    while err > epsilon and epoch < max_epochs:\n",
    "        epoch += 1\n",
    "        err, _ = sess.run([mean_error, train])\n",
    "        print('epoch: {0}  mean error: {1}'.format(epoch, err))\n",
    "        \n",
    "    print('Training complete')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tensorflow]",
   "language": "python",
   "name": "conda-env-tensorflow-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}